﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-03-23
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System.IO;
using UnityEditor;

namespace IFramework
{
	public class FormatUserScript
	{
        public static string NewScriptName { get { return newScriptName; } set { newScriptName = value; } }
        public static string OriginScriptPathWithOutNameSpace { get { return originScriptPathWithOutNameSpace; } set { originScriptPathWithOutNameSpace = value; } }
        public static string OriginScriptPathWithNameSpace { get { return originScriptPathWithNameSpace; } set { originScriptPathWithNameSpace = value; } }

        private static string newScriptName = "newScript.cs";
        private static string originScriptPathWithOutNameSpace = FrameWorkInfo.GetFrameWorkEditorPath().CombinePath(@"CopyAsset/User/CSharpScriptWithOutNameSpace.txt");
        private static string originScriptPathWithNameSpace = FrameWorkInfo.GetFrameWorkEditorPath().CombinePath(@"CopyAsset/User/CSharpScriptWithNameSpace.txt");


        [MenuItem("Assets/Create/FormatCSharpScript", false,1)]
        public static void CreateUIBase()
        {
            CreateOriginIfNull();
            if (ProjectInfo.projectSetting.IsUseNameSpace)
            {
                CopyAsset.CopyNewAsset(newScriptName, OriginScriptPathWithNameSpace);

            }
            else
            {
                CopyAsset.CopyNewAsset(newScriptName, OriginScriptPathWithOutNameSpace);
            }
        }
        private static void CreateOriginIfNullWithOutNameSpace()
        {
            if (File.Exists(originScriptPathWithOutNameSpace)) return;
            using (FileStream fs = new FileStream(originScriptPathWithOutNameSpace, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    fs.Lock(0, fs.Length);
                    sw.WriteLine("/*********************************************************************************");
                    sw.WriteLine(" *Author:         #User#");
                    sw.WriteLine(" *Version:        #UserVERSION#");
                    sw.WriteLine(" *UnityVersion:   #UserUNITYVERSION#");
                    sw.WriteLine(" *Date:           #UserDATE#");
                    sw.WriteLine(" *History:        #UserDATE#--");
                    sw.WriteLine("*********************************************************************************/");
                    sw.WriteLine("using System;");
                    sw.WriteLine("using System.Collections;");
                    sw.WriteLine("using System.Collections.Generic;");
                    sw.WriteLine("");

                    sw.WriteLine("public class #UserSCRIPTNAME#");
                    sw.WriteLine("{");
                    sw.WriteLine("");
                    sw.WriteLine("}");
                    fs.Unlock(0, fs.Length);
                    sw.Flush();
                    fs.Flush();
                }
            }

        }
        private static void CreateOriginIfNullWithNameSpace()
        {
            if (File.Exists(originScriptPathWithNameSpace)) return;
            using (FileStream fs = new FileStream(originScriptPathWithNameSpace, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    fs.Lock(0, fs.Length);
                    sw.WriteLine("/*********************************************************************************");
                    sw.WriteLine(" *Author:         #User#");
                    sw.WriteLine(" *Version:        #UserVERSION#");
                    sw.WriteLine(" *UnityVersion:   #UserUNITYVERSION#");
                    sw.WriteLine(" *Date:           #UserDATE#");
                    sw.WriteLine(" *Description:    #UserNameSpace#");
                    sw.WriteLine(" *History:        #UserDATE#--");
                    sw.WriteLine("*********************************************************************************/");
                    sw.WriteLine("using System;");
                    sw.WriteLine("using System.Collections;");
                    sw.WriteLine("using System.Collections.Generic;");
                    sw.WriteLine("");
                    sw.WriteLine("namespace #UserNameSpace#");
                    sw.WriteLine("{");
                    sw.WriteLine("\tpublic class #UserSCRIPTNAME#");
                    sw.WriteLine("\t{");
                    sw.WriteLine("\t");
                    sw.WriteLine("\t}");
                    sw.WriteLine("}");
                    fs.Unlock(0, fs.Length);
                    sw.Flush();
                    fs.Flush();
                }
            }

        }
        private static void CreateOriginIfNull()
        {

            CreateOriginIfNullWithNameSpace();
            CreateOriginIfNullWithOutNameSpace();
            AssetDatabase.Refresh();
        }
    }
}
